const app = getApp();
Page({
  data: {
    printer_name: "未选择打印机"
  },
  onLoad: function (options) {
  },
  onShow: function () {
    if (!app.globalData.isLogin) {
      wx.reLaunch({
        url: '/pages/wode/index',
      })
      return;
    }
    let printer = app.globalData.printer;
    if (printer == undefined) {
      wx.showModal({
        title: "提示",
        content: "请先选择打印机",
        showCancel: false,
        confirmText: "去选择",
        success: res2 => {
          if (res2.confirm) {
            wx.navigateTo({
              url: '../shop/index',
            })
          }
        }
      })
    } else {
      this.setData({
        printer_name: `${app.globalData.printer.printerName}(${app.globalData.printer.shopName})`,
        printer: app.globalData.printer,
      })
    }
  },
  // 点击微信文档和微信图片的时候触发
  selectWechatFile: function (e) {
    let type = e.currentTarget.dataset.type;
    console.log(type)
    wx.chooseMessageFile({
      count: 1,
      type: type,
      extension: ["doc", "docx", "xls", "xlsx", "pdf", "txt", "ppt", "pptx"],
      success: res => {
        for (let i = 0; i < res.tempFiles.length; i++) {
          let file = {
            path: res.tempFiles[i]["path"],
            filename: res.tempFiles[i]["name"]
          };
          app.upload(file, this.data.printer.id).then(data => {
            data = data.data

            let fileListTmp = wx.getStorageSync('fileListTmp')
            if(!wx.getStorageSync('fileListTmp')){
              fileListTmp = new Object();
            }
            fileListTmp[data.id] = file.path
            wx.removeStorageSync('fileListTmp');
            wx.setStorageSync('fileListTmp', fileListTmp);
            wx.navigateTo({
              url: '../print_list/index?type=sc&fileId=' + data.id,
            })
          }, err => {
            wx.showModal({
              title: "错误",
              content: (file.filename + err.msg) || (file.filename + JSON.stringify(err))
            })
            console.log(err)
          })
        }
      }
    })
  },
  onShareAppMessage() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let mini = res.data.mini;
        share = {
          title: mini.title,
          path: "/pages/index/index",
          imageUrl: mini.image,
        }
      }
    })
    return share;
  },
  onShareTimeline() {
    let share = {};
    wx.getStorage({
      key: 'share',
      success: res => {
        let momnet = res.data.momnet;
        share = {
          title: momnet.title,
          query: {
            printer_id: this.data.printer.id || 0
          },
          imageUrl: momnet.image,
        }
      }
    })
    return share;
  }
})